//
//  ReadSecDetailViewController.m
//  LuckyRadio
//
//  Created by Mr、J on 15/11/12.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "ReadSecDetailViewController.h"
#import "CustomerNetworking.h"
#import "ReadSecDetailModel.h"
#import "ReadSecDetailTableViewCell.h"
#import "ReadThirdDetailViewController.h"
#import "MJRefresh.h"
#import <MBProgressHUD.h>
@interface ReadSecDetailViewController ()<UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource, MBProgressHUDDelegate>
@property (nonatomic, retain) UISegmentedControl * seg;
@property (nonatomic, retain) NSMutableArray * segArr;
@property (nonatomic, retain) UIView * view1;
@property (nonatomic, retain) UIView * view2;
@property (nonatomic, retain) UIScrollView * bigScrollView;
@property (nonatomic, retain) NSMutableArray * readNewArr;
@property (nonatomic, retain) UITableView * readNewTable;

@property (nonatomic, retain) NSMutableArray * readHotArr;
@property (nonatomic, retain) UITableView * readHotTable;
@property (nonatomic, retain) MBProgressHUD *HUD;

@end

@implementation ReadSecDetailViewController
-(void)dealloc
{
    [_seg release];
    [_segArr release];
    [_view1 release];
    [_view2 release];
    [_bigScrollView release];
    [_readNewArr release];
    [_readNewTable release];
    [_readHotArr release];
    [_readHotTable release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"文章详情";
    //初始化
    self.readNewArr = [NSMutableArray array];
    self.readHotArr = [NSMutableArray array];
    //创建Seg条
    [self createSeg];
    //创建背景View
    [self createScrollView];
    //创建两个视图
    [self createViews];
    
    //第一页页面创建
    [self createNew];
    //第二页页面创建
    [self createHot];
    
    
    //第一页数据解析
    [self loadNewWithType:NO];
    //第二页数据解析
    [self loadHotWithType:NO];
    
    
    [self header];
    [self footer];
}

-(void) header
{
    self.readHotTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.readHotArr removeAllObjects];
        _offset =  0;
        [self loadHotWithType:NO];
    }];
    [self.readHotTable.mj_header beginRefreshing];

    self.readNewTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.readNewArr removeAllObjects];
        _offset =  0;
        [self loadNewWithType:NO];
    }];
    [self.readNewTable.mj_header beginRefreshing];


}
-(void) footer
{
    self.readNewTable.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self.readNewTable.mj_footer beginRefreshing];
        _offset += 10;
        [self loadNewWithType:YES];
    }];
//    [self.readNewTable.mj_footer beginRefreshing];

    self.readHotTable.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self.readHotTable.mj_footer beginRefreshing];
        _offset += 10;
        [self loadHotWithType:YES];
    }];
    [self.readHotTable.mj_footer beginRefreshing];

}

- (void)createNew
{
    self.readNewTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view1.frame.size.height) style:UITableViewStylePlain];
    self.readNewTable.delegate = self;
    self.readNewTable.dataSource = self;
    [self.view1 addSubview:self.readNewTable];
    [_readNewTable release];
}

- (void)createHot
{
    self.readHotTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view2.frame.size.height) style:UITableViewStylePlain];
    self.readHotTable.delegate = self;
    self.readHotTable.dataSource = self;
    [self.view2 addSubview:self.readHotTable];
    [_readHotTable release];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (tableView == self.readHotTable) {
        if (_readHotArr.count) {
            return self.readHotArr.count;
        }
    }else
    {
        if (_readNewArr.count) {
            return self.readNewArr.count;

        }
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 180;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadSecDetailTableViewCell * cell = [ReadSecDetailTableViewCell readSecDetailTableViewCellWithTableView:tableView];
    if (tableView == self.readNewTable) {
        if (_readNewArr.count) {
        ReadSecDetailModel * model = [self.readNewArr objectAtIndex:indexPath.row];
        cell.model = model;
            cell.backgroundColor = [UIColor clearColor];
            return cell;

        }
        return nil;
    }else
    {
        if (_readHotArr.count) {
        ReadSecDetailModel * model = [self.readHotArr objectAtIndex:indexPath.row];
        cell.model = model;
            cell.backgroundColor = [UIColor clearColor];
            return cell;

        }
        return nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ReadThirdDetailViewController * detail = [[ReadThirdDetailViewController alloc]init];
    if (tableView == self.readHotTable) {
        if (_readHotArr) {
            ReadSecDetailModel * model = [self.readHotArr objectAtIndex:indexPath.row];
            detail.tid = model.ID;
        }
        
    }else
    {
        if (_readNewArr) {
            ReadSecDetailModel * model = [self.readNewArr objectAtIndex:indexPath.row];
            detail.tid = model.ID;
        }
        
    }
    
    [self.navigationController pushViewController:detail animated:YES];
}
- (void) loadNewWithType:(BOOL)isMore
{
    if (_offset == 0) {
        self.HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
        self.HUD.delegate = self;
        //显示的文字
        self.HUD.labelText = @"正在进行加载~";
    }
    
    NSDictionary *body = @{@"client":@"1", @"deviceid":@"7F95D47F-BF62-4528-B18E-EE921FFC5323", @"limit":@"10", @"sort":@"addtime", @"start":[NSString stringWithFormat:@"%ld", _offset], @"typeid": [NSString stringWithFormat:@"%@", _readId], @"version":@"3.0.6"};
    [CustomerNetworking customerWithURL:@"http://api2.pianke.me/read/columns_detail" andParamer:body andBlock:^(id response) {
        if (isMore == NO) {
            if (!self.readNewArr.count) {
                for (NSDictionary * dic  in [[response objectForKey:@"data"]objectForKey:@"list"]) {
                    ReadSecDetailModel * model = [ReadSecDetailModel modelWithDictionary:dic];
                    [self.readNewArr addObject:model];
                }
                [self.readNewTable reloadData];
            }
        }else
        {
        for (NSDictionary * dic  in [[response objectForKey:@"data"]objectForKey:@"list"]) {
            ReadSecDetailModel * model = [ReadSecDetailModel modelWithDictionary:dic];
            [self.readNewArr addObject:model];
        }
        [self.readNewTable reloadData];
        }
    }];
    [self.readNewTable reloadData];
    [self.readNewTable.mj_header endRefreshing];
    [self.readNewTable.mj_footer endRefreshing];
    [self.HUD hide:YES];


}
- (void) loadHotWithType:(BOOL)isMore
{
    
    NSDictionary *body = @{@"client":@"1", @"deviceid":@"7F95D47F-BF62-4528-B18E-EE921FFC5323", @"limit":@"10", @"sort":@"hot", @"start":[NSString stringWithFormat:@"%ld", _offset], @"typeid": [NSString stringWithFormat:@"%@", _readId], @"version":@"3.0.6"};
    [CustomerNetworking customerWithURL:@"http://api2.pianke.me/read/columns_detail" andParamer:body andBlock:^(id response) {
        if (isMore == NO) {
            if (!self.readHotArr.count) {
                for (NSDictionary * dic  in [[response objectForKey:@"data"]objectForKey:@"list"]) {
                    ReadSecDetailModel * model = [ReadSecDetailModel modelWithDictionary:dic];
                    [self.readHotArr addObject:model];
                }
                [self.readHotTable reloadData];
            }
        }else
        {
        for (NSDictionary * dic  in [[response objectForKey:@"data"]objectForKey:@"list"]) {
            ReadSecDetailModel * model = [ReadSecDetailModel modelWithDictionary:dic];
            [self.readHotArr addObject:model];
        }
        [self.readHotTable reloadData];
        }
        [self.readHotTable reloadData];
        [self.readHotTable.mj_header endRefreshing];
        [self.readHotTable.mj_footer endRefreshing];
    }];
    

}
#pragma mark 背景滑动视图创建
-(void)createScrollView
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.bigScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height - 30 - 64)];
    //可滚动区域
    self.bigScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    self.bigScrollView.delegate = self;
    //边界不回弹
    self.bigScrollView.bounces = NO;
    self.bigScrollView.pagingEnabled = YES;
    //关闭垂直滚动条
    self.bigScrollView.showsVerticalScrollIndicator = NO;
    self.bigScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.bigScrollView];
    [self.bigScrollView release];
}

#pragma mark  创建两个独立的视图
-(void)createViews
{
    self.view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.bigScrollView.frame.size.height)];
    //创建
    self.view2 = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.bigScrollView.frame.size.height)];
    [self.bigScrollView addSubview:self.view1];
    [self.bigScrollView addSubview:self.view2];
    [_view1 release];
    [_view2 release];
}
#pragma mark  创建seg
-(void)createSeg
{
    self.segArr = [NSMutableArray arrayWithObjects:@"NEW", @"HOT", nil];
    //创建一个UISegmentedControl
    self.seg = [[UISegmentedControl alloc]initWithItems:self.segArr];
    self.seg.frame = CGRectMake(0, 0, self.view.frame.size.width, 30);
    self.seg.tintColor = [UIColor blackColor];
    [self.seg addTarget:self action:@selector(changeAction:) forControlEvents:UIControlEventValueChanged];
    self.seg.selectedSegmentIndex = 0;
    [self.view addSubview:self.seg];
    [_seg release];

}
#pragma mark  seg点击事件实现
- (void)changeAction:(UISegmentedControl *)seg
{
    switch (seg.selectedSegmentIndex) {
        case 0:
            self.bigScrollView.contentOffset = CGPointMake(0, 0);
            break;
        case 1:
            self.bigScrollView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
            break;
        default:
            break;
    }

}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if ([[NSString stringWithFormat:@"%@", [scrollView class]] isEqualToString:@"UIScrollView"]) {
        self.seg.selectedSegmentIndex = self.bigScrollView.contentOffset.x / self.view.frame.size.width;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
